package me.feng.service

import me.feng.dao.SessionFactoryHelper


abstract class BaseService  {
  
  def getSession = SessionFactoryHelper.getSession
  
  def withTransaction[T](f: => T): T = warpSession(f)

  private def warpSession[T](f: => T): T = {
    if(getSession.getTransaction().wasCommitted())
    	getSession.getTransaction().begin()
    println("Transaction begin")
    val t = f
    if(getSession.getTransaction().isActive())
    	getSession.getTransaction().commit()
    getSession.close()
    println("Transaction commit")
    t
  }
}